<template>
	<view class="modular">
		<view class="tips">请在90秒内快速回答以下问题</view>
		<view class="content">
			<view class="time">{{num}}s</view>
			<view class="questionModular">
				<view class="question" v-for="(item,index) in questionnaire" :key="index">
					<view class="title">
						<text>0{{index+1}}</text>
						<view>{{item.name}}</view>
					</view>
					<view class="option" v-for="(aitem,aindex) in item.adTopicsRepliesEntityList" :key="aindex">
						<view @click="checkChange(aitem,item)">
							<text>{{aitem.elections}}、{{aitem.value}}</text> 
							<image src="@/static/img/select@2x.png" v-if="!aitem.checked"></image>  
							<image src="@/static/img/selected@2x.png" v-else></image>  
						</view>
					</view>
				</view>
			</view>	
			<view class="sure" @click="sure">确认</view>
			<view class="transparent1"></view>
			<view class="transparent2"></view>
		</view>
		<u-modal v-model="showPopup" @confirm="previousPage" :content="showContent"></u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				num:90,
				questionnaire:[],
				showPopup: false,
				showContent: '',
			}
		},
		onShow(){
			this.num = 90;
			this.findByTopicsTypeAxios();
			this.time()
		},
		methods:{
			sure() {
				// let unCheckarr = []
				let Checkarr = []
				this.questionnaire.map((a)=>{
					a.adTopicsRepliesEntityList.map((b)=>{
						if(b.checked){
							a.elections = b.elections
							a.setvalue = b.value
							Checkarr.push(b)
						}
					})
				})
				if(Checkarr.length == 4){
					this.clearTimer()
					var isA = []
					isA = Checkarr.filter((v)=>{return v.elections == 'A'})
					if(isA.length > 0){
						localStorage.setItem('questionnaire',JSON.stringify(this.questionnaire))
						uni.navigateTo({
							url:'/pages/quickly/healthStatus/anxious/index'
						})
					}else{
						uni.navigateTo({
							url:'/pages/quickly/healthStatus/anxious/state'
						})
					}
				}else{
					uni.showToast({
						title:'请完整填写问卷。',
						icon: 'none'
					})
				}
			},
			// 90s倒计时
			time(){
				const _this = this;
				_this.timer = setInterval(()=>{
					if(_this.num <= 0){
						_this.showPopup = true;
						_this.showContent = '未在规定时间内做题,点击确定后返回上一页';
						_this.timer && _this.clearTimer();
						return;
					}
					_this.num--
				},1000)
			},
			clearTimer() {//清除定时器
				clearInterval(this.timer);
				this.timer = null;
			},
			previousPage() {
				uni.navigateTo({
					url:'/pages/quickly/healthStatus/navigatorBar'
				})
			},
			// 获取题目
			findByTopicsTypeAxios(){
				this.$api.findByTopicsType('ids=25').then((res)=>{
					this.questionnaire = res.data.result['25'].adTopicsRepliesEntityList.map((a)=>{
						a.adTopicsRepliesEntityList = a.adTopicsRepliesEntityList.map((b)=>{ b.checked = false; return b;})
						return a
					})
				})
			},
			checkChange(aitem,item){
				item.adTopicsRepliesEntityList.map((a)=>{
					a.checked = false
				})
				aitem.checked = true
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import '@/static/css/quickly/healthStatus/screening.scss';
</style>
